function image=HSItoRGB (H,S,I)
[X, Y, Z]=size(I) ;

image=zeros (X, Y,3) ;
R=zeros (X, Y) ;
G=zeros (X, Y) ;
B=zeros (X, Y) ;

for i=1 :X
    for j=1:Y
        if (H(i,j)<=(2*pi/3) )
            b=I(i,j)* (1-S(i,j)) ;
            r=I(i,j)* (1+S(i,j)*cos(H(i,j))/cos(pi/3-H(i,j))) ;
            g=3*I(i,j)-R(i,j)-B(i,j) ;
            image(i,j,1)=r;
            image(i,j,2)=g;
            image(i,j,3)=b;
        else if(H(i, j)<=(4*pi/3) )
                H=H-2*pi/3;
                b=3*I(i,j)-R(i,j)-G(i,j);
                r=I(i,j)*(1-S(i,j));
                g=I(i,j)* (1+S(i,j) *cos(H(i,j)) /cos (pi/3-H(i,j))) ;
                image(i,j,1)=r;
                image(i,j,2)=g;
                image(i, j,3)=b;
        else if (H(i,j)<=2*pi)
                    H=H-4*pi/3;
                    g=I(i,j)*(1-S(i,j)) ;
                    b=I(i,j)* (1+S(i,j)*cos(H(i,j))/cos(pi/3-H(i,j))) ;
                    r=3*I(i,j)-R(i,j)-G(i,j);
                    image(i,j,1)=r;
                    image (i,j,2)=g;
                    image(i,j,3)=b;
                end
            end
        end
    end
end
